package com.atguigu.gulimall.product.controller;


import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.IPmsAttrService;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.vo.AttrRespVO;
import com.atguigu.gulimall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author szh
 * @since 2020-12-01
 */

@RestController
@RequestMapping("product/attr")
public class PmsAttrController {
	@Autowired
	private IPmsAttrService attrService;

	@Autowired
	private ProductAttrValueService productAttrValueService;

	// /product/attr/base/list/{catelogId} 获取基本属性， 通过一个字段的不同取值来决定是什么属性
	// /product/attr/sale/list/{catelogId} 获取销售属性，
	@GetMapping("/{attrType}/list/{catelogId}")
	public R listAttrList(@RequestParam Map<String, Object> params,
	                      @PathVariable("catelogId")Long catelogId,
	                      @PathVariable("attrType")String type){
		PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);
		return R.ok().put("page", page);
	}
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = attrService.queryPage(params);

		return R.ok().put("page", page);
	}
	// /product/attr/base/listforspu/{spuId} 第22个
	public R baseAttrListForSpu(@PathVariable Long spuId){
		List<ProductAttrValueEntity> entities =productAttrValueService.baseAttrListForSpu(spuId);
		return R.ok().put("data",entities);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{attrId}")
	public R info(@PathVariable("attrId") Long attrId){
//		PmsAttr pmsAttr = pmsAttrService.getById(attrId);
		AttrRespVO respVO = attrService.getAttrInfo(attrId);

		return R.ok().put("pmsAttr", respVO);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody AttrVO pmsAttr){
		attrService.saveAttr(pmsAttr);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody AttrVO attrVO){
		attrService.updateAttr(attrVO);

		return R.ok();
	}
	//  /product/attr/update/{spuId} 23
	@PostMapping("/update/{spuId}")
	public R update(@PathVariable Long spuId,
	                @RequestBody List<ProductAttrValueEntity> entities){
		 productAttrValueService.updateAttr(spuId,entities);
		 return R.ok();

	}
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

		return R.ok();
	}



}

